package com.phiture.erp.produce.core.handler;

import cn.hutool.core.collection.CollUtil;
import com.phiture.erp.produce.core.service.ErpMaterialRequirementPlanService;
import com.phiture.erp.produce.dal.entity.ErpMaterialRequirementPlanItemsDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.phiture.erp.common.constants.ErrorCodeConstants.MRP_RELEASED_FAIL_INFO_ERROR;

/**
 * @author YH
 * @date 2025-05-13-14:20
 */

@Slf4j
@Component
public class BaseMrpTaskDispatchHandler {
    @Resource
    private ErpMaterialRequirementPlanService erpMaterialRequirementPlanService;

    protected List<ErpMaterialRequirementPlanItemsDO> getErpMaterialRequirementPlanItems(Collection<Long> itemIds){
        List<ErpMaterialRequirementPlanItemsDO> items = erpMaterialRequirementPlanService.getByItemIds(itemIds);
        if(CollUtil.isEmpty(items)){
            log.error("[BaseMrpTaskDispatchHandler.getErpMaterialRequirementPlanItems] 未查询到MRP明细信息， itemIds:{}", itemIds);
            throw exception(MRP_RELEASED_FAIL_INFO_ERROR);
        }
        return items;
    }

    protected void backfillItemsStatus(Collection<Long> itemIds){
        erpMaterialRequirementPlanService.backfillItemsStatusToReleased(itemIds);
    }
}
